home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / libs / info / man.c < prev    next >
C/C++ Source or Header  |  1995-11-28  |  16KB  |  692 lines

  1. /*  man.c: How to read and format man files. */
  2. /* Changed for emx by Kai Uwe Rommel & Eberhard Mattes -- Nov 1995 */
  3.  
  4. /* This file is part of GNU Info, a program for reading online documentation
  5.    stored in Info format.
  6.  
  7.    Copyright (C) 1995 Free Software Foundation, Inc.
  8.  
  9.    This program is free software; you can redistribute it and/or modify
  10.    it under the terms of the GNU General Public License as published by
  11.    the Free Software Foundation; either version 2, or (at your option)
  12.    any later version.
  13.  
  14.    This program is distributed in the hope that it will be useful,
  15.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.    GNU General Public License for more details.
  18.  
  19.    You should have received a copy of the GNU General Public License
  20.    along with this program; if not, write to the Free Software
  21.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22.  
  23.    Written by Brian Fox Thu May  4 09:17:52 1995 (bfox@ai.mit.edu). */
  24.  
  25. #include "info.h"
  26. #include <sys/ioctl.h>
  27. #include <sys/file.h>
  28. #include "signals.h"
  29. #if defined (HAVE_SYS_TIME_H)
  30. #include <sys/time.h>
  31. #endif
  32. #if defined (HAVE_SYS_WAIT_H)
  33. #include <sys/wait.h>
  34. #endif
  35. #ifdef EMX
  36. #include <process.h>
  37. #include <sys/wait.h>
  38. #endif /* EMX */
  39. #include "tilde.h"
  40.  
  41. #include "man.h"
  42.  
  43. #if !defined (_POSIX_VERSION)
  44. #define pid_t int
  45. #endif
  46.  
  47. #if defined (FD_SET)
  48. #  if defined (hpux)
  49. #    define fd_set_cast(x) (int *)(x)
  50. #  else
  51. #    define fd_set_cast(x) (fd_set *)(x)
  52. #  endif /* !hpux */
  53. #endif /* FD_SET */
  54.  
  55. static char *read_from_fd ();
  56. static void clean_manpage ();
  57. static NODE *manpage_node_of_file_buffer ();
  58. static char *get_manpage_contents ();
  59.  
  60. NODE *
  61. make_manpage_node (pagename)
  62.      char *pagename;
  63. {
  64.   return (info_get_node (MANPAGE_FILE_BUFFER_NAME, pagename));
  65. }
  66.  
  67. NODE *
  68. get_manpage_node (file_buffer, pagename)
  69.      FILE_BUFFER *file_buffer;
  70.      char *pagename;
  71. {
  72.   NODE *node;
  73.  
  74.   node = manpage_node_of_file_buffer (file_buffer, pagename);
  75.  
  76.   if (!node)
  77.     {
  78.       char *page;
  79.  
  80.       page = get_manpage_contents (pagename);
  81.  
  82.       if (page)
  83.     {
  84.       char header[1024];
  85.       long oldsize, newsize;
  86.       int hlen, plen;
  87.  
  88.       sprintf (header, "\n\n%c\n%s %s,  %s %s,  %s (dir)\n\n",
  89.            INFO_COOKIE,
  90.            INFO_FILE_LABEL, file_buffer->filename,
  91.            INFO_NODE_LABEL, pagename,
  92.            INFO_UP_LABEL);
  93.       oldsize = file_buffer->filesize;
  94.       hlen = strlen (header);
  95.       plen = strlen (page);
  96.       newsize = (oldsize + hlen + plen);
  97.       file_buffer->contents =
  98.         (char *)xrealloc (file_buffer->contents, 1 + newsize);
  99.       memcpy (file_buffer->contents + oldsize, header, hlen);
  100.       oldsize += hlen;
  101.       memcpy (file_buffer->contents + oldsize, page, plen);
  102.       file_buffer->contents[newsize] = '\0';
  103.       file_buffer->filesize = newsize;
  104.       file_buffer->finfo.st_size = newsize;
  105.       build_tags_and_nodes (file_buffer);
  106.       free (page);
  107.     }
  108.  
  109.       node = manpage_node_of_file_buffer (file_buffer, pagename);
  110.     }
  111.  
  112.   return (node);
  113. }
  114.  
  115. FILE_BUFFER *
  116. create_manpage_file_buffer ()
  117. {
  118.   FILE_BUFFER *file_buffer;
  119.   struct stat *finfo;
  120.  
  121.   file_buffer = make_file_buffer ();
  122.   file_buffer->filename = strdup (MANPAGE_FILE_BUFFER_NAME);
  123.   file_buffer->fullpath = strdup (MANPAGE_FILE_BUFFER_NAME);
  124.   file_buffer->finfo.st_size = 0;
  125.   file_buffer->filesize = 0;
  126.   file_buffer->contents = (char *)NULL;
  127.   file_buffer->flags = (N_IsInternal | N_CannotGC | N_IsManPage);
  128.   
  129.   return (file_buffer);
  130. }
  131.  
  132. /* Scan the list of directories in PATH looking for FILENAME.  If we find
  133.    one that is an executable file, return it as a new string.  Otherwise,
  134.    return a NULL pointer. */
  135. static char *
  136. executable_file_in_path (filename, path)
  137.      char *filename, *path;
  138. {
  139.   struct stat finfo;
  140.   char *temp_dirname;
  141.   int statable, dirname_index;
  142.  
  143.   dirname_index = 0;
  144.  
  145.   while (temp_dirname = extract_colon_unit (path, &dirname_index))
  146.     {
  147.       register int i;
  148.       char *temp;
  149.  
  150.       /* Expand a leading tilde if one is present. */
  151.       if (*temp_dirname == '~')
  152.     {
  153.       char *expanded_dirname;
  154.  
  155.       expanded_dirname = tilde_expand_word (temp_dirname);
  156.       free (temp_dirname);
  157.       temp_dirname = expanded_dirname;
  158.     }
  159.  
  160.       temp = (char *)xmalloc (30 + strlen (temp_dirname) + strlen (filename));
  161.       strcpy (temp, temp_dirname);
  162.       if (temp[(strlen (temp)) - 1] != '/')
  163.     strcat (temp, "/");
  164.       strcat (temp, filename);
  165.  
  166.       free (temp_dirname);
  167.  
  168. #ifdef EMX
  169.       strcat (temp, ".cmd");
  170.       statable = (stat (temp, &finfo) == 0);
  171.       if (!statable)
  172.       {
  173.     strcpy(temp + strlen (temp) - 4, ".exe");
  174.       statable = (stat (temp, &finfo) == 0);
  175.       }
  176. #else /* !EMX */
  177.       statable = (stat (temp, &finfo) == 0);
  178. #endif /* !EMX */
  179.  
  180.       /* If we have found a regular executable file, then use it. */
  181.       if ((statable) && (S_ISREG (finfo.st_mode)) &&
  182.       (access (temp, X_OK) == 0))
  183.     return (temp);
  184.       else
  185.     free (temp);
  186.     }
  187.   return ((char *)NULL);
  188. }
  189.  
  190. /* Return the full pathname of the system man page formatter. */
  191. static char *
  192. find_man_formatter ()
  193. {
  194.   return (executable_file_in_path ("man", (char *)getenv ("PATH")));
  195. }
  196.  
  197. static char *manpage_pagename = (char *)NULL;
  198. static char *manpage_section  = (char *)NULL;
  199.  
  200. static void
  201. get_page_and_section (pagename)
  202.      char *pagename;
  203. {
  204.   register int i;
  205.  
  206.   if (manpage_pagename)
  207.     free (manpage_pagename);
  208.  
  209.   if (manpage_section)
  210.     free (manpage_section);
  211.  
  212.   manpage_pagename = (char *)NULL;
  213.   manpage_section  = (char *)NULL;
  214.  
  215.   for (i = 0; pagename[i] != '\0' && pagename[i] != '('; i++);
  216.  
  217.   manpage_pagename = (char *)xmalloc (1 + i);
  218.   strncpy (manpage_pagename, pagename, i);
  219.   manpage_pagename[i] = '\0';
  220.  
  221.   if (pagename[i] == '(')
  222.     {
  223.       int start;
  224.  
  225.       start = i + 1;
  226.  
  227.       for (i = start; pagename[i] != '\0' && pagename[i] != ')'; i++);
  228.  
  229.       manpage_section = (char *)xmalloc (1 + (i - start));
  230.       strncpy (manpage_section, pagename + start, (i - start));
  231.       manpage_section[i - start] = '\0';
  232.     }
  233. }
  234.  
  235. static void
  236. reap_children (sig)
  237.      int sig;
  238. {
  239.   unsigned int status;
  240.   wait (&status);
  241. }
  242.  
  243. static char *
  244. get_manpage_contents (pagename)
  245.      char *pagename;
  246. {
  247.   static char *formatter_args[4] = { (char *)NULL };
  248.   int pipes[2];
  249.   pid_t child;
  250.   char *formatted_page = (char *)NULL;
  251.   char *section = (char *)NULL;
  252.   int arg_index = 1;
  253.  
  254.   if (formatter_args[0] == (char *)NULL)
  255.     formatter_args[0] = find_man_formatter ();
  256.  
  257.   if (formatter_args[0] == (char *)NULL)
  258.     return ((char *)NULL);
  259.  
  260.   get_page_and_section (pagename);
  261.  
  262.   if (manpage_section != (char *)NULL)
  263.     formatter_args[arg_index++] = manpage_section;
  264.  
  265.   formatter_args[arg_index++] = manpage_pagename;
  266.   formatter_args[arg_index] = (char *)NULL;
  267.  
  268.   /* Open a pipe to this program, read the output, and save it away
  269.      in FORMATTED_PAGE.  The reader end of the pipe is pipes[0]; the
  270.      writer end is pipes[1]. */
  271.   pipe (pipes);
  272.  
  273. #ifdef EMX
  274.   {
  275.     int pid, old_stdout, old_stderr;
  276.     
  277.     old_stdout = dup (fileno (stdout));
  278.     fcntl (old_stdout, F_SETFD, FD_CLOEXEC);
  279.     dup2 (pipes[1], fileno (stdout));
  280.  
  281.     old_stderr = dup (fileno (stderr));
  282.     fcntl (old_stderr, F_SETFD, FD_CLOEXEC);
  283.     close (fileno (stderr));
  284.     
  285.     close (pipes[1]);
  286.     fcntl (pipes[0], F_SETFD, FD_CLOEXEC);
  287.  
  288.     pid = spawnv (P_NOWAIT, formatter_args[0], formatter_args);
  289.  
  290.     close (fileno (stdout));
  291.     dup2 (old_stdout, fileno (stdout));
  292.     close (old_stdout);
  293.  
  294.     dup2 (old_stderr, fileno (stderr));
  295.     close (old_stderr);
  296.  
  297.     if (pid != -1)
  298.       formatted_page = read_from_fd (pipes[0]);
  299.  
  300.     close (pipes[0]);
  301.  
  302.     waitpid (pid, NULL, WNOHANG);
  303.   }
  304. #else /* !EMX */
  305.   signal (SIGCHLD, reap_children);
  306.  
  307.   child = fork ();
  308.  
  309.   if (child == -1)
  310.     return ((char *)NULL);
  311.   
  312.   if (child != 0)
  313.     {
  314.       /* In the parent, close the writing end of the pipe, and read from
  315.      the exec'd child. */
  316.       close (pipes[1]);
  317.       formatted_page = read_from_fd (pipes[0]);
  318.       close (pipes[0]);
  319.     }
  320.   else
  321.     {
  322.       /* In the child, close the read end of the pipe, make the write end
  323.      of the pipe be stdout, and execute the man page formatter. */
  324.       close (pipes[0]);
  325.       close (fileno (stderr));
  326.       close (fileno (stdin));    /* Don't print errors. */
  327.       dup2 (pipes[1], fileno (stdout));
  328.  
  329.       execv (formatter_args[0], formatter_args);
  330.  
  331.       /* If we get here, we couldn't exec, so close out the pipe and
  332.      exit. */
  333.       close (pipes[1]);
  334.       exit (0);
  335.     }
  336. #endif /* !EMX */
  337.  
  338.   /* If we have the page, then clean it up. */
  339.   if (formatted_page)
  340.     clean_manpage (formatted_page);
  341.  
  342.   return (formatted_page);
  343. }
  344.  
  345. static void
  346. clean_manpage (manpage)
  347.      char *manpage;
  348. {
  349.   register int i, j;
  350.   int newline_count = 0;
  351.   char *newpage;
  352.  
  353.   newpage = (char *)xmalloc (1 + strlen (manpage));
  354.  
  355.   for (i = 0, j = 0; newpage[j] = manpage[i]; i++, j++)
  356.     {
  357.       if (manpage[i] == '\n')
  358.     newline_count++;
  359.       else
  360.     newline_count = 0;
  361.  
  362.       if (newline_count == 3)
  363.     {
  364.       j--;
  365.       newline_count--;
  366.     }
  367.  
  368.       if (manpage[i] == '\b')
  369.     j -= 2;
  370.     }
  371.  
  372.   newpage[j++] = '\0';
  373.  
  374.   strcpy (manpage, newpage);
  375.   free (newpage);
  376. }
  377.  
  378. static NODE *
  379. manpage_node_of_file_buffer (file_buffer, pagename)
  380.      FILE_BUFFER *file_buffer;
  381.      char *pagename;
  382. {
  383.   NODE *node = (NODE *)NULL;
  384.   TAG *tag = (TAG *)NULL;
  385.  
  386.   if (file_buffer->contents)
  387.     {
  388.       register int i;
  389.  
  390.       for (i = 0; tag = file_buffer->tags[i]; i++)
  391.     {
  392.       if (strcasecmp (pagename, tag->nodename) == 0)
  393.         break;
  394.     }
  395.     }
  396.  
  397.   if (tag)
  398.     {
  399.       node = (NODE *)xmalloc (sizeof (NODE));
  400.       node->filename = file_buffer->filename;
  401.       node->nodename = tag->nodename;
  402.       node->contents = file_buffer->contents + tag->nodestart;
  403.       node->nodelen = tag->nodelen;
  404.       node->flags    = 0;
  405.       node->parent   = (char *)NULL;
  406.       node->flags = (N_HasTagsTable | N_IsManPage);
  407.       node->contents += skip_node_separator (node->contents);
  408.     }
  409.  
  410.   return (node);
  411. }
  412.  
  413. static char *
  414. read_from_fd (fd)
  415.      int fd;
  416. {
  417.   struct timeval timeout;
  418.   char *buffer = (char *)NULL;
  419.   int bsize = 0;
  420.   int bindex = 0;
  421.   int select_result;
  422. #if defined (FD_SET)
  423.   fd_set read_fds;
  424.  
  425.   timeout.tv_sec = 15;
  426.   timeout.tv_usec = 0;
  427.  
  428.   FD_ZERO (&read_fds);
  429.   FD_SET (fd, &read_fds);
  430.  
  431.   select_result = select (fd + 1, fd_set_cast (&read_fds), 0, 0, &timeout);
  432. #else /* !FD_SET */
  433.   select_result = 1;
  434. #endif /* !FD_SET */
  435.  
  436.   switch (select_result)
  437.     {
  438.     case 0:
  439.     case -1:
  440.       break;
  441.  
  442.     default:
  443.       {
  444.         int amount_read;
  445.         int done = 0;
  446.  
  447.         while (!done)
  448.           {
  449.             while ((bindex + 1024) > (bsize))
  450.               buffer = (char *)xrealloc (buffer, (bsize += 1024));
  451.             buffer[bindex] = '\0';
  452.  
  453.             amount_read = read (fd, buffer + bindex, 1023);
  454.  
  455.             if (amount_read < 0)
  456.               {
  457.                 done = 1;
  458.               }
  459.             else
  460.               {
  461.                 bindex += amount_read;
  462.                 buffer[bindex] = '\0';
  463.                 if (amount_read == 0)
  464.                   done = 1;
  465.               }
  466.           }
  467.       }
  468.     }
  469.  
  470.   if ((buffer != (char *)NULL) && (*buffer == '\0'))
  471.     {
  472.       free (buffer);
  473.       buffer = (char *)NULL;
  474.     }
  475.  
  476.   return (buffer);
  477. }
  478.  
  479. static char *reference_section_starters[] =
  480. {
  481.   "\nRELATED INFORMATION",
  482.   "\nRELATED\tINFORMATION",
  483.   "RELATED INFORMATION\n",
  484.   "RELATED\tINFORMATION\n",
  485.   "\nSEE ALSO",
  486.   "\nSEE\tALSO",
  487.   "SEE ALSO\n",
  488.   "SEE\tALSO\n",
  489.   (char *)NULL
  490. };
  491.  
  492. static SEARCH_BINDING frs_binding;
  493.  
  494. static SEARCH_BINDING *
  495. find_reference_section (node)
  496.      NODE *node;
  497. {
  498.   register int i;
  499.   long position = -1;
  500.  
  501.   frs_binding.buffer = node->contents;
  502.   frs_binding.start = 0;
  503.   frs_binding.end = node->nodelen;
  504.   frs_binding.flags = S_SkipDest;
  505.  
  506.   for (i = 0; reference_section_starters[i] != (char *)NULL; i++)
  507.     {
  508.       position = search_forward (reference_section_starters[i], &frs_binding);
  509.       if (position != -1)
  510.     break;
  511.     }
  512.  
  513.   if (position == -1)
  514.     return ((SEARCH_BINDING *)NULL);
  515.  
  516.   /* We found the start of the reference section, and point is right after
  517.      the string which starts it.  The text from here to the next header
  518.      (or end of buffer) contains the only references in this manpage. */
  519.   frs_binding.start = position;
  520.  
  521.   for (i = frs_binding.start; i < frs_binding.end - 2; i++)
  522.     {
  523.       if ((frs_binding.buffer[i] == '\n') &&
  524.       (!whitespace (frs_binding.buffer[i + 1])))
  525.     {
  526.       frs_binding.end = i;
  527.       break;
  528.     }
  529.     }
  530.  
  531.   return (&frs_binding);
  532. }
  533.  
  534. REFERENCE **
  535. xrefs_of_manpage (node)
  536.      NODE *node;
  537. {
  538.   SEARCH_BINDING *reference_section;
  539.   REFERENCE **refs = (REFERENCE **)NULL;
  540.   int refs_index = 0;
  541.   int refs_slots = 0;
  542.   long position;
  543.  
  544.   reference_section = find_reference_section (node);
  545.  
  546.   if (reference_section == (SEARCH_BINDING *)NULL)
  547.     return ((REFERENCE **)NULL);
  548.  
  549.   /* Grovel the reference section building a list of references found there.
  550.      A reference is alphabetic characters followed by non-whitespace text
  551.      within parenthesis. */
  552.   reference_section->flags = 0;
  553.  
  554.   while ((position = search_forward ("(", reference_section)) != -1)
  555.     {
  556.       register int start, end;
  557.  
  558.       for (start = position; start > reference_section->start; start--)
  559.     if (whitespace (reference_section->buffer[start]))
  560.       break;
  561.  
  562.       start++;
  563.  
  564.       for (end = position; end < reference_section->end; end++)
  565.     {
  566.       if (whitespace (reference_section->buffer[end]))
  567.         {
  568.           end = start;
  569.           break;
  570.         }
  571.  
  572.       if (reference_section->buffer[end] == ')')
  573.         {
  574.           end++;
  575.           break;
  576.         }
  577.     }
  578.  
  579.       if (end != start)
  580.     {
  581.       REFERENCE *entry;
  582.       int len = end - start;
  583.  
  584.       entry = (REFERENCE *)xmalloc (sizeof (REFERENCE));
  585.       entry->label = (char *)xmalloc (1 + len);
  586.       strncpy (entry->label, (reference_section->buffer) + start, len);
  587.       entry->label[len] = '\0';
  588.       entry->filename = strdup (node->filename);
  589.       entry->nodename = strdup (entry->label);
  590.       entry->start = start;
  591.       entry->end = end;
  592.  
  593.       add_pointer_to_array
  594.         (entry, refs_index, refs, refs_slots, 10, REFERENCE *);
  595.     }
  596.  
  597.       reference_section->start = position + 1;
  598.     }
  599.  
  600.   return (refs);
  601. }
  602.  
  603. long
  604. locate_manpage_xref (node, start, dir)
  605.      NODE *node;
  606.      long start;
  607.      int dir;
  608. {
  609.   register int i, count;
  610.   REFERENCE **refs;
  611.   long position = -1;
  612.  
  613.   refs = xrefs_of_manpage (node);
  614.  
  615.   if (refs)
  616.     {
  617.       register int i, count;
  618.       REFERENCE *entry;
  619.  
  620.       for (i = 0; refs[i]; i++);
  621.       count = i;
  622.  
  623.       if (dir > 0)
  624.     {
  625.       for (i = 0; entry = refs[i]; i++)
  626.         if (entry->start > start)
  627.           {
  628.         position = entry->start;
  629.         break;
  630.           }
  631.     }
  632.       else
  633.     {
  634.       for (i = count - 1; i > 0; i--)
  635.         {
  636.           entry = refs[i];
  637.  
  638.           if (entry->start < start)
  639.         {
  640.           position = entry->start;
  641.           break;
  642.         }
  643.         }
  644.     }
  645.  
  646.       info_free_references (refs);
  647.     }
  648.   return (position);
  649. }
  650.  
  651. /* This one was a little tricky.  The binding buffer that is passed in has
  652.    a START and END value of 0 -- strlen (window-line-containing-point).
  653.    The BUFFER is a pointer to the start of that line. */
  654. REFERENCE **
  655. manpage_xrefs_in_binding (node, binding)
  656.      NODE *node;
  657.      SEARCH_BINDING *binding;
  658. {
  659.   register int i;
  660.   REFERENCE **all_refs = xrefs_of_manpage (node);
  661.   REFERENCE **brefs = (REFERENCE **)NULL;
  662.   REFERENCE *entry;
  663.   int brefs_index = 0;
  664.   int brefs_slots = 0;
  665.   int start, end;
  666.  
  667.   if (!all_refs)
  668.     return ((REFERENCE **)NULL);
  669.  
  670.   start = binding->start + (binding->buffer - node->contents);
  671.   end = binding->end + (binding->buffer - node->contents);
  672.  
  673.   for (i = 0; entry = all_refs[i]; i++)
  674.     {
  675.       if ((entry->start > start) && (entry->end < end))
  676.     {
  677.       add_pointer_to_array
  678.         (entry, brefs_index, brefs, brefs_slots, 10, REFERENCE *);
  679.     }
  680.       else
  681.     {
  682.       maybe_free (entry->label);
  683.       maybe_free (entry->filename);
  684.       maybe_free (entry->nodename);
  685.       free (entry);
  686.     }
  687.     }
  688.  
  689.   free (all_refs);
  690.   return (brefs);
  691. }
  692.